﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuocViet.WebUI.Models.Interface;
using Project.Domain.Entities;
using Framework.EntityFramework;

namespace QuocViet.WebUI.Models.Repository
{
    public class UserbetRepository:IUserbetRepository
    {
        private readonly DomainContext _db = new DomainContext();
        public void AddUserbet(UserBet userbet)
        {
            _db.UserBets.Add(userbet);
            _db.SaveChanges();
        }


        public BetDetail GetBetdetailById(int betdetailid)
        {
            return _db.BetDetails.FirstOrDefault(b => b.BetDetailId == betdetailid);
        }


        public bool CheckUserbetOfBetdetail(int betdatailid, int UserId)
        {
            var userbetofbetdetail = _db.UserBets.FirstOrDefault(b => b.BetDetailId == betdatailid && b.UserId == UserId && b.Status == false);
            return userbetofbetdetail == null;
        }


        public IEnumerable<UserBet> GetAllBetOfUser(int UserId)
        {
            return _db.UserBets
                .Where(b => b.UserId == UserId && b.Status == false).Take(5);
        }


        public bool Deluserbet(int userbetid)
        {
            try
            {
                var userbet = _db.UserBets.SingleOrDefault(b => b.UserBetId == userbetid);
                _db.UserBets.Remove(userbet);
                _db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }            
        }
    }   
}